<template>
  <div>
    <!-- 面包屑导航部分 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>权限管理</el-breadcrumb-item>
    <el-breadcrumb-item>角色列表</el-breadcrumb-item>
  </el-breadcrumb>
    <!-- 卡片部分 -->
  <el-card class="el-card">
    <el-button type="primary" @click="addDialogVisable=true">添加角色</el-button>
      <el-table :data="rolesList" border stripe>
        <!-- 展开列 -->
      <el-table-column type="expand">
      <template slot-scope="scope">
        <el-row v-for="item in scope.row.children" :key="item.id" class="el-row1">
          <!-- 一级的权限 -->
          <el-col :span="5">
            <el-tag>{{item.authName}}<em class="el-icon-close" @click="setDeleteStatus(scope.row.id,item.id)"></em></el-tag>

            <em class="el-icon-caret-right"></em>
          </el-col>
           <!-- 二级的权限及三级权限 -->
          <el-col :span="19">
            <el-row v-for="item1 in item.children" :key="item1.id" class="el-row2">
              <el-col :span="6">
                <el-tag type="success">{{item1.authName}}<em class="el-icon-close" @click="setDeleteStatus(scope.row.id,item1.id)"></em></el-tag>
                <em class="el-icon-caret-right"></em>
              </el-col>
              <el-col :span="18">
                <el-tag type="warning" v-for="(item2,index) in item1.children" :key="item2.id" class="el-tag">{{item2.authName}}<em class="el-icon-close" @click="setDeleteStatus(scope.row.id,item2.id)"></em></el-tag>
              </el-col>
            </el-row>
          </el-col >

        </el-row>
      </template>
      </el-table-column>
        <!-- 索引列 -->
      <el-table-column type="index" ></el-table-column>
      <el-table-column label="角色名称" prop="roleName"></el-table-column>
      <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="editDialogVisable=true" type="primary" icon="el-icon-edit" plain size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" plain size="mini" @click="setDeleteStatus(scope.row.id,0)">删除</el-button>
          <el-button type="warning" icon="el-icon-setting" plain size="mini" @click="showSetRightDialog(scope.row)">分配权限</el-button>
        </template>
      </el-table-column>
    </el-table>

  </el-card>
<!-- 添加用户的对话框 -->
  <el-dialog
  title="添加用户"
  :visible.sync="addDialogVisable"
  width="70%"
  >
  <!-- 输入用户信息的栏目 -->
  <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px" class="demo-ruleForm">
  <el-form-item label="角色名称" prop="roleName">
    <el-input v-model="addForm.roleName"></el-input>
  </el-form-item>

  <el-form-item label="角色描述" prop="roleDesc">
    <el-input v-model="addForm.roleDesc"></el-input>
  </el-form-item>

  </el-form>
<!-- 这是取消和确定的两个按钮 -->
  <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisable = false">取 消</el-button>
    <el-button type="primary" @click="addRole">确 定</el-button>
  </span>
</el-dialog>

<!-- 编辑用户的对话框 -->
  <el-dialog
  title="编辑角色"
  :visible.sync="editDialogVisable"
  width="70%"
  >
  <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px" class="demo-ruleForm">
  <el-form-item label="角色名称" prop="roleName">
    <el-input v-model="addForm.roleName"></el-input>
  </el-form-item>

  <el-form-item label="角色描述" prop="roleDesc">
    <el-input v-model="addForm.roleDesc"></el-input>
  </el-form-item>

  </el-form>
<!-- 这是取消和确定的两个按钮 -->
  <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisable= false">取 消</el-button>
    <el-button type="primary" @click="editDialogVisable= false">确 定</el-button>
  </span>
</el-dialog>

<!-- 删除角色的对话框 -->
<el-dialog
  title="提示"
  :visible.sync="deleteDialogVisible"
  width="70%"
  >
  <!-- 输入用户信息的栏目 -->
  <em class="el-icon-warning"></em>此操作将永久删除该用户，是否继续？
<!-- 这是取消和确定的两个按钮 -->
  <span slot="footer" class="dialog-footer">
    <el-button @click="deleteDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="deleteRightFuc">确 定</el-button>
  </span>
</el-dialog>

<!-- 设置分配权限的对话框 -->
<el-dialog
  title="提示"
  :visible.sync="setRightDialogVisable"
  width="70%"
  >
  <!-- <el-checkbox-group v-model="rightsList">
    <el-checkbox :label="item.authName" v-for="item in rightsList" :key="item.id" class="el-checkbox">
      {{item.authName}}

      <el-checkbox v-if="item.children" v-for="item1 in item.children" :key="item1.id" class="el-checkbox">
        {{item1.authName}}
        <el-checkbox v-if="item1.children" v-for="item2 in item1.children" :key="item2.id" class="el-checkbox">
       {{item2.authName}}
       </el-checkbox>
      </el-checkbox>
    </el-checkbox>
  </el-checkbox-group> -->
<el-tree ref="treeRef" :data="rightsList" :props="defaultProps" show-checkbox node-key="id" :default-expand-all="true" :default-checked-keys="defKeys"></el-tree>

<!-- 这是取消和确定的两个按钮 -->
  <span slot="footer" class="dialog-footer">
    <el-button @click="setRightDialogVisable = false">取 消</el-button>
    <el-button type="primary" @click="setNewRights">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>
<script>
export default {
  data(){
    return {
    rolesList:[],
    rightsList:[],
    addDialogVisable:false,
    editDialogVisable:false,
    deleteDialogVisible:false,
    setRightDialogVisable:false,
    defKeys:[],
    roleID:'',
    addForm:{
      roleName:'',
      roleDesc :''
    },
    defaultProps:{
       children: 'children',
        label: 'authName'
    },
    deleteRight:{
      roleId:'',
      rightId:''
    },
  // 添加用户的验证规则
     addFormRules:{
        roleName:[{required:true, message:"角色名不能为空",trigger:'blur'},
        {min:3,max:10,message:"角色名的长度在3到10之间",trigger:'blur'}],
        roleDesc:[
        {min:0,max:15,message:"用户名的长度在0到15之间",trigger:'blur'}],
      }
    }
  },
  created(){
    this.getRolesList();
  },
  methods:{
    async getRolesList(){
      const {data:res} = await this.$http.get('roles');
      if(res.meta.status != 200) {
        return this.$message.error("获取角色列表失败！");
      }
      this.rolesList = res.data;

    },
    addRole(){
      this.$refs.addFormRef.validate(async valid =>{
        if(!valid) return;
        const {data:res} = await this.$http.post('roles',this.addForm);
        if(res.meta.status != 201){
          return this.$message.error("添加角色失败！")
        }
        this.$message.success("添加角色成功");
        this.addDialogVisable = false;
        this.getRolesList();
      })
    },
   setDeleteStatus(roleId,rightId){
     this.deleteRight.rightId = rightId;
     this.deleteRight.roleId = roleId;
     this.deleteDialogVisible = true;

   },
   async deleteRightFuc(){
     if(this.deleteRight.rightId == 0){
       const {data:res} = await this.$http.delete(`roles/${this.deleteRight.roleId}`,this.deleteRight.roleId);
       if(res.meta.status != 200) {
     return this.$message.error("删除用户权限失败！");
     }
     this.$message.success("删除用户权限成功!");
     this.deleteDialogVisible = false;
     this.getRolesList();

     }

     const {data:res} = await this.$http.delete(`roles/${this.deleteRight.roleId}/rights/${this.deleteRight.rightId}`,
     this.deleteRight
     );
     if(res.meta.status != 200) {
     return this.$message.error("删除用户权限失败！");
     }
     this.$message.success("删除用户权限成功!");
     this.deleteDialogVisible = false;
     this.getRolesList();
   },
   async showSetRightDialog(node){
     this.defKeys = [];
     this.getDefaultId(node);
     this.roleID = node.id;

     this.setRightDialogVisable = true;

     const {data:res} = await this.$http.get('rights/tree');
     if(res.meta.status != 200) {
       return this.$message.error("获取权限列表失败")
     }
     this.$message.success("获取权限列表成功");
     console.log(res.data);
     this.rightsList = res.data;
   },
   getDefaultId(node){
     if(!node.children){
      return  this.defKeys.push(node.id)
     }
     node.children.forEach(item =>{
       this.getDefaultId(item);
     })
   },
   async setNewRights(){
     const keys = [...this.$refs.treeRef.getCheckedKeys(),...this.$refs.treeRef.getHalfCheckedKeys()];
     console.log(keys);
     const idStr = keys.join(',');

     const {data:res} = await this.$http.post(`roles/${this.roleID}/rights`,{rids:idStr})
    if(res.meta.status != 200) {
      return this.$message.error("更新权限失败!")
    }
    this.$message.success("更新权限成功！")
    this.getRolesList();
    this.setRightDialogVisable = false;

   }


  }
}
</script>

<style scoped>
  .el-card{
    margin-top:10px
  }
  .el-row1{
    height: 140px;
    border-bottom: 1px solid black;
    display: flex;
    align-items: center;
  }
  .el-tag{
    margin-right: 5px;
  }
  .el-row2{
    border-bottom: 1px solid grey;
  }
  .el-checkbox{
    display:block;
  }
</style>
